// 04myCenter/04wasteOrder/wasteOrder.js
var co = require('../../resource/js/common.js')
var ws = require('../../resource/js/websocket.js')
var app = getApp()
var _this, _data
var pageNum = 1
Page({
   /**
    * 页面的初始数据
    */
   data: {
      bizList: '', //企业列表
      //标签列表
      taps: [{
            name: '新约订单',
            id: 6
         },
         {
            name: '待上门',
            id: 5
         },
         {
            name: '待确认',
            id: 4
         },
         {
            name: '待评价',
            id: 2
         },
         {
            name: '全部',
            id: -1
         },
      ],
      id: '',//标签页切换ID
      userInfo: '',//用户信息
      allWasteOrder:[],//回收订单
      hint: false,
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function(options) {
      _this = this
      _data = this.data
      var orderStatu = 6     
      this.setData({
         userInfo: app.Data.userInfo,
         id:orderStatu
      })
   },
   // 获取导航条点击id
   getOrederStatus(event) {
      var idx = event.currentTarget.dataset.index
      _this.setData({
         id: idx,
         allWasteOrder: [],
         hint:false
      })
      var id = parseInt(idx)
      //切换标签时，页码置零
      pageNum = 1     
      this.getWasteOrder(id)
   },
   //获取订单
   getWasteOrder: function (orderStatus){
      var url = 'order/Order/getOrderList'
      var param = {
         Biz_ID: -1,
         User_ID: _data.userInfo.User_ID,
         Order_Status: orderStatus,
         page: pageNum
      }
      co.getJyhsApi(url,param,function(res){
         if(res.code == 0){
            pageNum++
            var list = _data.allWasteOrder.concat(res.data)
            _this.setData({
               allWasteOrder:list
            })
         }else{
            _this.setData({
               hint: true,
            })
         }
      })
   },
   //订单操作
   operateOrder:function(e){
      var url = 'order/Order/updateOrderStatue'
      var status = e.currentTarget.dataset.status
      var orderID = e.currentTarget.dataset.order
      if( status == 1 || status == 0){
         var doType = 'cancelOrder'
         var afterStatus = -1
      }else if( status == 4){
         var doType = 'finishOrder'
         var afterStatus = 2
      }      
      var param = {
         Order_ID :orderID,
         Order_Status : afterStatus 
      }
      co.getJyhsApi(url,param,function(res){                  
         if(res.code == 0){
            wx.showToast({
               title: '订单操作成功',
               icon: 'success',
               duration: 1000
            })
            pageNum = 1
            _this.setData({
               allWasteOrder: []
            })
            _this.getWasteOrder(_data.id)//取消成功重新获取订单
            _this.ws_send(doType, res.data.Reclaim_User_ID)//取消成功发送websocke
         }
      })
   },
   //用户发送web
   ws_send: function (doType,Reclaim_User_ID) {
      var msg = {
         type: doType,//'cancelOrder',
         reclaim_id: Reclaim_User_ID,
      }
      ws.sendSocketMsg(JSON.stringify(msg), function (res) {
         console.log('【sendSocketMsg:发送成功回调（111）】')
      })
   },
   //评价
   toEvaluate:function(e){
      var orderID = e.currentTarget.dataset.order.Order_ID
      var recID = e.currentTarget.dataset.order.Reclaim_User_ID
      wx.navigateTo({
         url: '/04myCenter/04wasteOrder/01wasteOrderEvaluate/wasteOrderEvaluate?orderID=' + orderID + '&recID=' + recID,
      })
   },
   //查看订单详情
   checkWasteOrder:function(e){
      var orderID = e.currentTarget.dataset.orderid
      wx.navigateTo({
         url: '/04myCenter/04wasteOrder/02wasteOrderDetail/wasteOrderDetail?orderID='+orderID,
      })
   },
   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function() {
      pageNum = 1
      _this.setData({
         allWasteOrder:[]
      })
      var id = _data.id
      this.getWasteOrder(id)
   },
})